// pages/StoreSearch/StoreSearch.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    inputs: '',
    pageNum: 1,
    goods: [], //goodList 数据
    goload: false, //loadmore--if
    styleObj: {
      display: 'inline-block',
      marginRight: '25px',
      backgroundColor: 'white',
      marginLeft: '5px',
      position: 'static'
    },
    jumpSo: true,
    store_id: '',
    loading: false, //goodList--boolean
    allLoaded: true, //loadmore-all,
    classifyBol: true,
    showHistory:false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
    if(options.goods_ids){
      this.getIds(options.goods_ids);
    }else{
      this.gotoSearch()
    }

  },
  serachTag(event) {
    let value = event.detail;
    this.setData({
      inputs:value,
    });
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  getIds(ids){
    let that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-goods-by-ids');
    urlStr += '&store_id=' + this.data.store_id;
    urlStr += '&goods_ids=' + ids;
    if(this.options.cid)  urlStr += '&category_id=' + this.options.cid;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            pageNum: Number(that.data.pageNum) + 1,
            loading: false,
            allLoaded: false
          });
          that.setData({
            goods: res.data.data
          });
					
          if (res.data.current_page == res.data.last_page||res.data.total <= 0) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
        } else {
          that.setData({
            loading: true,
            allLoaded: true
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    // this.gotoSearch();
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    this.gotoSearch()
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val,
    });
  },
  inpFocus(e){
    this.setData({
      showHistory:true
    });
  },
  inpBlur(e){
    let val = e.detail.value;
    this.setData({
      inputs: val,
      showHistory:false
    });
  },
  search() {
    this.setData({
      pageNum: 1,
      goods: [],
      goload: true
    });
    this.gotoSearch();
  },
  //搜索请求
  gotoSearch() {
    if(this.data.inputs){
      this.selectComponent("#search_history").appendKeywords(this.data.inputs);
    }
    let that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-goods-by-kwd');
    urlStr += '&store_id=' + this.data.store_id;
    urlStr += '&kwd=' + this.data.inputs;
    urlStr += '&page=' + this.data.pageNum;
    if(this.options.cid)  urlStr += '&category=' + this.options.cid;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            pageNum: Number(that.data.pageNum) + 1,
            loading: false,
            allLoaded: false
          });
          let goods = that.data.goods;
          that.setData({
            goods: goods.concat(res.data.data)
          });
					
          if (res.data.current_page == res.data.last_page ||res.data.total <= 0) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
        } else {
          that.setData({
            loading: true,
            allLoaded: true
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  classifyShowBtn() {
    if (this.data.classifyBol) {
      this.setData({
        classifyBol: false
      });
    } else {
      this.setData({
        classifyBol: true
      });
    }
  }
});
